package com.itheima.appserver.excepiton;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @项目名称: tanhua-53
 * @包名: com.itheima.appserver.excepiton
 * @作者: 李云飞
 * @日期: 2021-12-03
 */
@ControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(MyException.class)
    public ResponseEntity myEx(MyException myException) {
        myException.printStackTrace();
        // 响应500 并且把错误对象返回
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(myException.getErrorResult());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity ex(Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
